package com.example.my_music_palyer.controller;

import com.example.my_music_palyer.model.Music;
import com.example.my_music_palyer.model.User;
import com.example.my_music_palyer.service.LoveMusicService;
import com.example.my_music_palyer.common.tools.Constant;
import com.example.my_music_palyer.common.tools.ResponseBodyMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@RequestMapping("/lovemusic")
@RestController
@ResponseBody
public class LoveMusicContreller {

    @Autowired
    LoveMusicService loveMusicService;

    @RequestMapping("/likeMusic")
    ResponseBodyMessage<Boolean> likeMusic(@RequestParam String id,
                                           HttpServletRequest req){
        //校验登陆
        HttpSession session = req.getSession(false);
        if (session == null || session.getAttribute(Constant.USERINFO_SESSION_KEY) == null) {
            return new ResponseBodyMessage<>(-1, "请登陆后在收藏", false);
        }

        User user = (User) req.getSession().getAttribute(Constant.USERINFO_SESSION_KEY);
        int userId = user.getId();
        int musicId = Integer.parseInt(id);
        Music music = loveMusicService.findLoveMusicByMusicIdAndUserId(userId,musicId);

        if(music != null){
            return new ResponseBodyMessage<>(-1,"该用户已收藏此歌曲",false);
        }else {
            int ret = loveMusicService.insertLoveMusic(userId,musicId);
            if(ret == 1) {
                return new ResponseBodyMessage<>(200,"收藏歌曲成功",true);
            }else {
                return  new ResponseBodyMessage<>(-1,"收藏歌曲失败",false);
            }
        }
    }

    @RequestMapping("/findlovemusic")
    ResponseBodyMessage<List<Music>> findlovemusic(
            @RequestParam( required = false) String musicName,HttpServletRequest req){
        //校验登陆
        HttpSession session = req.getSession(false);
        if (session == null || session.getAttribute(Constant.USERINFO_SESSION_KEY) == null) {
            return new ResponseBodyMessage<>(-1, "请登陆后再查询收藏歌曲", null);
        }
        User user = (User) req.getSession().getAttribute(Constant.USERINFO_SESSION_KEY);
        int userId = user.getId();

        List<Music> music = null;
        if(musicName == null) {
            music = loveMusicService.findLoveMusicByUserId(userId);
        }else {
            music = loveMusicService.findLoveMusicBykeyAndUID(musicName,userId);
        }
        return new ResponseBodyMessage<>(200, "查询收藏歌曲成功", music);

    }

    @RequestMapping("/deletelovemusic")
    ResponseBodyMessage<Boolean> deletelovemusic(@RequestParam String id
                                                    ,HttpServletRequest req) {
        //校验登陆
        HttpSession session = req.getSession(false);
        if (session == null || session.getAttribute(Constant.USERINFO_SESSION_KEY) == null) {
            return new ResponseBodyMessage<>(-1, "请登陆后再查询收藏歌曲", null);
        }
        User user = (User) req.getSession().getAttribute(Constant.USERINFO_SESSION_KEY);
        int userId = user.getId();

        int musicId =Integer.parseInt(id);
        int ret = loveMusicService.deleteLoveMusic(userId,musicId);
        if(ret == 1) {
            return new ResponseBodyMessage<>(200,"取消收藏成功",true);
        }else {
            return new ResponseBodyMessage<>(-1,"取消收藏失败", false);

        }
    }

}
